package Swing.src.puzzle.GameUI;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
import java.util.Random;

public class GameJFrame extends JFrame implements ActionListener, MouseListener, KeyListener {

    String path = "img\\lty2\\";
    int[][] data = new int[4][4];
    int[][] win = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16},};
    int step = 0;
    int x;
    int y;
    //选项条目
    JMenuItem replayItem = new JMenuItem("重新开始");
    JMenuItem reLoginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    JMenuItem qqItem = new JMenuItem("球球");
    JMenuItem D1Item = new JMenuItem("洛天依1");
    JMenuItem D2Item = new JMenuItem("洛天依2");
    public GameJFrame() {
//初始化界面
        InitJFrame();
//初始化菜单
        InitJMenubar();
//        初始化数据
        InitData();
//        初始化图片
        InitImage();
//总是显示
        this.setVisible(true);
    }

    private void InitData() {
        int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random random = new Random();
        for (int i = 0; i < tempArr.length; i++) {
            int index = random.nextInt(tempArr.length);
            int tmp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = tmp;
        }
        for (int i = 0; i < tempArr.length; i++) {
            if (tempArr[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
            data[i / 4][i % 4] = tempArr[i];
        }
    }

    private void InitImage() {
        this.getContentPane().removeAll();
        if (victory()) {
            JLabel jLabel = new JLabel(new ImageIcon("img\\win.png"));
            jLabel.setBounds(200, 130, 260, 260);
            this.getContentPane().add(jLabel);
        }
        JLabel CountStep = new JLabel("步数:" + step);
        CountStep.setBounds(30, 30, 100, 20);
        this.getContentPane().add(CountStep);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int num = data[i][j];
                JLabel jLabel = new JLabel(new ImageIcon("" + path + num + ".png"));
                jLabel.setBounds(105 * j + 120, 105 * i + 80, 105, 105);
                jLabel.setBorder(new BevelBorder(0));
                this.getContentPane().add(jLabel);
            }
        }
        InitBkg();
    }

    private void InitBkg() {
        JLabel jLabel = new JLabel(new ImageIcon("img\\bkg\\blue.png"));
        jLabel.setBounds(0, 0, 680, 640);
        this.getContentPane().add(jLabel);
        this.getContentPane().repaint();
    }

    private Boolean victory() {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (data[i][j] != win[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    private void InitJMenubar() {
        JMenuBar jMenuBar = new JMenuBar();
//菜单选项
        JMenu functionJMenu = new JMenu("选项");
        JMenu ChangeImg = new JMenu("选择图片");
        JMenu aboutJMenu = new JMenu("关于");

//        添加条目到选项
        ChangeImg.add(D1Item);
        ChangeImg.add(D2Item);
        functionJMenu.add(ChangeImg);
        functionJMenu.add(replayItem);
//        functionJMenu.add(reLoginItem);
        functionJMenu.add(closeItem);
        aboutJMenu.add(qqItem);
        //        为条目绑定事件
        D1Item.addActionListener(this);
        D1Item.addMouseListener(this);
        D2Item.addActionListener(this);
        replayItem.addActionListener(this);
        reLoginItem.addActionListener(this);
        closeItem.addActionListener(this);
        qqItem.addActionListener(this);
//        添加选项到菜单
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);
//设置菜单
        this.setJMenuBar(jMenuBar);
    }

    private void InitJFrame() {
        //        初始设置
        this.setSize(680, 640);
        this.setTitle("洛天依のGame v0.1");
//        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null); //设置窗体位置
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置关闭操作
        this.setLayout(null);  //取消默认布局
        this.setFocusable(true); //获取焦点
        //        键盘事件
        this.addKeyListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == replayItem) {
            System.out.println("重新开始");
            restart();
        } else if (source == reLoginItem) {
            this.setVisible(false);
//            new LoginJFrame();
            System.out.println("重新登录");
        } else if (source == closeItem) {
            System.out.println("关闭游戏");
            System.exit(0);
        } else if (source == qqItem) {
            JDialog jDialog = new JDialog();
            jDialog.setTitle("炸娃凭吐给母 v0.1");
            JLabel jLabel = new JLabel(new ImageIcon("img\\qq.png"));
            JLabel jLabel1 = new JLabel("移动:a w d s/← ↑ → ↓");
            JLabel jLabel2 = new JLabel("重开:space");
            JLabel jLabel3 = new JLabel("查看原图:Q");
            JLabel jLabel4 = new JLabel("通关:R");
            JLabel jLabel5 = new JLabel("你认为你是炸娃凭吐搞兽马");
            jLabel.setBounds(0, 0, 400, 400);
            jLabel1.setBounds(10, 20, 200, 80);
            jLabel2.setBounds(10, 40, 200, 80);
            jLabel3.setBounds(10, 60, 200, 80);
            jLabel4.setBounds(10, 80, 200, 80);
            jLabel5.setBounds(10, 100, 200, 80);
            jDialog.getContentPane().add(jLabel1);
            jDialog.getContentPane().add(jLabel2);
            jDialog.getContentPane().add(jLabel3);
            jDialog.getContentPane().add(jLabel4);
            jDialog.getContentPane().add(jLabel5);
            jDialog.getContentPane().add(jLabel);
            jDialog.setSize(400, 400);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setVisible(true);
            System.out.println("球球号:" + "2644056893");
        } else if (source == D1Item) {
            System.out.println("洛天依1");
            path = "img\\lty1\\";
            restart();
        } else if (source == D2Item) {
            System.out.println("洛天依2");
            path = "img\\lty2\\";
            restart();
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        System.out.println("单击");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("按住不松");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("松开");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("划入");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.println("离开");
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (victory()) return;
        int keyCode = e.getKeyCode();
        if (keyCode == 81) {
            this.getContentPane().removeAll();
            JLabel jLabel = new JLabel(new ImageIcon("" + path + "all.png"));
            jLabel.setBounds(120, 80, 420, 420);
            jLabel.setBorder(new BevelBorder(0));
            this.getContentPane().add(jLabel);
        }
        InitBkg();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (victory()) return;
        int keyCode = e.getKeyCode();
        System.out.println(keyCode);
        if (keyCode == 37 || keyCode == 65) {
            try {
                System.out.println("left");
                data[x][y] = data[x][y + 1];
                data[x][y + 1] = 0;
                y++;
                step++;
                InitImage();
            } catch (ArrayIndexOutOfBoundsException ex) {
                System.out.println("lefted");
            }
        } else if (keyCode == 38 || keyCode == 87) {
            try {
                System.out.println("up");
                data[x][y] = data[x + 1][y];
                data[x + 1][y] = 0;
                x++;
                step++;
                InitImage();
            } catch (ArrayIndexOutOfBoundsException ex) {
                System.out.println("top");
            }
        } else if (keyCode == 39 || keyCode == 68) {
            try {
                System.out.println("right");
                data[x][y] = data[x][y - 1];
                data[x][y - 1] = 0;
                y--;
                step++;
                InitImage();
            } catch (ArrayIndexOutOfBoundsException ex) {
                System.out.println("righted");
            }
        } else if (keyCode == 40 || keyCode == 83) {
            try {
                System.out.println("down");
                data[x][y] = data[x - 1][y];
                data[x - 1][y] = 0;
                x--;
                step++;
                InitImage();
            } catch (ArrayIndexOutOfBoundsException ex) {
                System.out.println("bottom");
            }
        } else if (keyCode == 81) {
            InitImage();
        } else if (keyCode == 82) {
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    data[i][j] = win[i][j];
                }
            }
            InitImage();
            x = 3;
            y = 3;
        } else if (keyCode == 32) {
            restart();
        }
    }

    //重新开始
    private void restart() {
        step = 0;
        InitData();
        InitImage();
    }
}
